<?php require_once('../../Connections/cotiza.php'); ?>
<?php
session_start();
mysql_select_db($database_cotiza, $cotiza);

$acc = $_GET['acc'];

if($acc == "save")
{
	$cod_familia  = $_POST['cod_familia'];
	$desc_familia = $_POST['desc_familia'];
	$id_proc	  = $_POST['proceso'];
	$bandera	  = $_POST['bandera'];
	$sustratos    = $_POST['sust'];
	$i=0;
	
	if($sustratos != "")
	{	
		$s_u = array_unique($sustratos);
		//$s_u = $sustratos;
		foreach($s_u as $sunicos)
			$s .= $sunicos.";";
	}
	$query_insert = "INSERT INTO fam_mstr(fam_id,fam_desc,fam_sustratos) VALUES ('$cod_familia','$desc_familia','$s') 
		             ON DUPLICATE KEY UPDATE fam_desc = '$desc_familia', fam_sustratos = '$s'; ";
	mysql_query($query_insert, $cotiza) or die(mysql_error());
		
	foreach($id_proc as $proc)
	{
		$req = 0;
		
		if($bandera[$i])
			$req = 1;
			
		$query_proc = "INSERT INTO fpro_det(fpro_fam_id,fpro_proc_id,fpro_req) VALUES ('$cod_familia','$proc',$req) 
		               ON DUPLICATE KEY UPDATE fpro_req = $req; ";
		mysql_query($query_proc, $cotiza) or die(mysql_error());
		$i++;
	}
	
	header("Location: mntofamilia.php");
}

if($acc == "edit")
{
	$cod_familia  = $_GET['cod_familia'];
	
	$query_familia = "SELECT * FROM fam_mstr where fam_id = '$cod_familia'";
	$familia = mysql_query($query_familia, $cotiza) or die(mysql_error());
	$row_familia = mysql_fetch_assoc($familia);
	
	$sustratos_def = split(';',$row_familia['fam_sustratos']);
	$c = count($sustratos_def);

	if($c >0)
		$c--;
	else
		$c = 1;
}
?>
<?php
$query_sustratos = "SELECT * FROM sus_mstr";
$sustratos = mysql_query($query_sustratos, $cotiza) or die(mysql_error());
$row_sustratos = mysql_fetch_assoc($sustratos);
$totalRows_sustratos = mysql_num_rows($sustratos);

$query_procesos = "SELECT * FROM proc_mstr";
$procesos = mysql_query($query_procesos, $cotiza) or die(mysql_error());
$row_procesos = mysql_fetch_assoc($procesos);
$totalRows_procesos = mysql_num_rows($procesos);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>...::: MNTO Familia :::...</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/funciones.js"></script>
<script language="javascript" type="text/javascript">
var indiceFilaFormulario=<?php echo $c+1; ?>;
var pct_capa=0;

function addCriterio(nombre){
 myNewRow = document.getElementById(nombre).insertRow(-1);
 myNewRow.id=indiceFilaFormulario;
 myNewCell=myNewRow.insertCell(-1);
 var indice = document.form1.sustratos.selectedIndex;
 
 var nuevoHide="";
 var nuevaLabel = document.form1.sustratos.value + " - " + document.form1.sustratos.options[indice].text;
 nuevoHide="<input type='hidden' name='sust["+indiceFilaFormulario+"]' value='" + document.form1.sustratos.value + "' />";
 myNewCell.innerHTML=nuevaLabel;
 myNewCell=myNewRow.insertCell(-1);
 myNewCell.innerHTML=nuevoHide;
 myNewCell=myNewRow.insertCell(-1);
 myNewCell.innerHTML="<a href='#' alt='Eliminar sustrato'><img src='../img/cancel.png' width='16' height='16' border='0' onclick='removeCriterio(this)'></a>";
 indiceFilaFormulario++;
 
}

function removeCriterio(obj){
 var oTr = obj;
 while(oTr.nodeName.toLowerCase()!='tr'){
  oTr=oTr.parentNode;
 }
 var root = oTr.parentNode;
 root.removeChild(oTr);
}
</script>
</head>

<body>
<strong>Ruta</strong>
<form id="form1" name="form1" method="post" action="mntofamilia.php?acc=save">
<table border="0">
  <tr>
    <td width="65">Cod. Ruta</td>
    <td width="10">:</td>
    <td width="97"><input name="cod_familia" type="text" class="default" id="cod_familia" size="20" autocomplete="off" <?php if($cod_familia != "") echo "readonly=\"readonly\" value=\"$cod_familia\""; else echo " onkeypress=\"check_key(event,'mntofamilia.php?acc=edit&cod_familia=' + this.value)\""; ?>/></td>
    <td width="98"><?php if($cod_familia == "") { ?><a href="javascript:abrir('../find/familias.php','cod_familia',370,450)"><img src="../img/find.png" width="16" height="16" border="0" /></a><?php } ?></td>
  </tr>
  <tr>
    <td>Descripci&oacute;n</td>
    <td>:</td>
    <td colspan="2"><input name="desc_familia" type="text" class="default" id="desc_familia" size="40" <?php if($cod_familia == "") echo "disabled=\"disabled\""; ?> value="<?php echo $row_familia['fam_desc']; ?>" /></td>
  </tr>
</table>
<script>
try{
	var foc = '<?php echo $cod_familia;?>';
	
	if(foc != "")
		document.getElementById('desc_familia').focus();
	else
		document.getElementById('cod_familia').focus();
}
catch(e)
{
}
</script>
<br />
<table border="0">
  <tr>
    <td width="250"><strong>Procesos</strong></td>
    <td>&nbsp;</td>
    <td width="270" align="left" valign="top" ><strong>Sustratos</strong></td>
    </tr>
  <tr>
    <td valign="top"><table border="0" align="left" cellpadding="0" cellspacing="0">
      <?php $i=0; do { 

	$pr    = $row_procesos['proc_id'];
	$query = "SELECT * FROM fpro_det WHERE fpro_fam_id = '$cod_familia' AND fpro_proc_id = $pr";
	$band  = mysql_query($query, $cotiza);
	$row   = mysql_fetch_assoc($band);
  ?>
      <tr>
        <td><?php echo $row_procesos['proc_id']; ?>.- </td>
        <td><?php echo $row_procesos['proc_desc']; ?></td>
        <td>:</td>
        <td><input name="bandera[<?php echo $i; ?>]" type="checkbox" id="bandera[<?php echo $i; ?>]" value="1" <?php if($cod_familia == "") echo "disabled=\"disabled\""; ?> <?php if($row['fpro_req'] == 1) echo "checked=\"checked\"";?> /></td>
        <td><input type="hidden" name="proceso[<?php echo $i; ?>]" id="proceso[<?php echo $i; ?>]" value="<?php echo $row_procesos['proc_id']; ?>" /></td>
      </tr>
      <?php $i++;} while ($row_procesos = mysql_fetch_assoc($procesos)); ?>
    </table></td>
    <td width="28">&nbsp;</td>
    <td align="left" valign="top" ><table width="259" border="0" id="sustratos">
      <tr>
        <td width="253" align="left"><select name="sustratos" class="default" id="sustratos">
          <?php
do {  
?>
          <option value="<?php echo $row_sustratos['sus_id']?>"><?php echo $row_sustratos['sus_desc']?></option>
          <?php
} while ($row_sustratos = mysql_fetch_assoc($sustratos));
  $rows = mysql_num_rows($sustratos);
  if($rows > 0) {
      mysql_data_seek($sustratos, 0);
	  $row_sustratos = mysql_fetch_assoc($sustratos);
  }
?>
          </select>
          <a href="javascript:addCriterio('sustratos')">
          <?php if($cod_familia != "") { ?>
          <img src="../img/plus.png" width="16" height="16" border="0" />
          <?php } ?>
          </a></td>
        </tr>
        <?php $csust = count($sustratos_def);
		if($csust > 0){
			for($j=0;$j<$csust-1;$j++)
			{
		?>
        <tr>
        	<td><?php $s = $sustratos_def[$j]; echo $s;
				$query = "SELECT * FROM sus_mstr WHERE sus_id = '$s' ";
				$band  = mysql_query($query, $cotiza);
				$row   = mysql_fetch_assoc($band);
				echo " - ".$row['sus_desc'];
			?></td>
            <td><input type="hidden" name="sust[<?php echo $j+1; ?>]" value="<?php echo $s; ?>" /></td>
            <td><a href='#' alt='Eliminar sustrato'><img src='../img/cancel.png' width='16' height='16' border='0' onclick='removeCriterio(this)'></a></td>
        </tr>
        <?php
			}
		}?>
    </table></td>
  </tr>
</table>
</form>
<br />
<table width="479" border="0">
  <tr>
    <td width="363" align="right"><?php if ($id_usuario == ""){?>
      <input name="button" type="button" onclick="ir('mntofamilia.php?acc=edit&cod_familia=' + document.getElementById('cod_familia').value,'_self')" class="default" id="button" value="Editar" />
      <?php }?></td>
    <td width="46" align="right"><input name="button2" type="button" onclick="window.open('mntofamilia.php','_self');" class="default" id="button2" value="Volver" <?php if($cod_familia == "") echo "disabled=\"disabled\""; ?> /></td>
    <td width="56" align="right"><input name="Submit" type="button" class="default" value="Guardar" onclick="document.forms.form1.submit()" <?php if($cod_familia == "") echo "disabled=\"disabled\""; ?> /></td>
    </tr>
</table>

</body>
</html>
<?php
mysql_free_result($sustratos);

mysql_free_result($procesos);
?>
<?php 
mysql_close($cotiza);
?>